package service.impl;

import mapper.SysUsersMapper;
import model.SysUsers;
import model.dto.SysUsersDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.SysUsersService;
import util.MD5Util;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class SysUsersServiceImpl implements SysUsersService {
    @Resource
    private SysUsersMapper mapper;
    @Override
    public List<SysUsers> findAll(SysUsersDto dto) {
        return mapper.findAll(dto);
    }

    @Override
    public int add(SysUsers users) {
        if(mapper.checkAccount(users.getAccount())+mapper.checkUserName(users.getUserName())>0){
            return 0;
        }
        return mapper.add(users);
    }

    @Override
    public SysUsers login(SysUsers users) {

        String password = users.getPassword();//取出用户登录时输入的密码
        SysUsers user = mapper.login(users);
        if(user==null){
            return null;
        }
        String salt = user.getSalt();
        String pwd = user.getPassword();//取出db里的密码
        if(MD5Util.md5(password+salt).equals(pwd)){//验证密码
            return user;
        }
        return null;
    }

    @Override
    public int getCount(SysUsersDto dto) {
        return mapper.getCount(dto);
    }

    @Override
    public int edit(SysUsers users) {
        return mapper.edit(users);
    }

    @Override
    public int del(int id) {
        return mapper.del(id);
    }

    @Override
    public int changeStatus(int id) {
        return mapper.changeStatus(id);
    }
}
